<?php
require_once ("../src/db/connectdatabase.php");

function getUuDais($namepro, $bankid, $uudai, $hethan, $ishethan) {
	$con = new ConnectDB();
	
	$tbl = "uudai";
	$fn = array('iduudai', 'typeuudai', 'tenuudai', 'nameproduct', 'infoview', 'saleoff', 'ngaybatdau', 'ngayhethan', 'content', 'diachilienhe', 'bankid', 'bankname', 'banklogo', 'loaithe', 'image', 'datecreate');
	$condetion = array('isuse' => true);
	if($ishethan == "1") {
		$condetion['ngayhethan <'] = $hethan;
	} else if($ishethan == "0") {
		$condetion['ngayhethan >'] = $hethan;
	}
	$flike = array('nameproduct' => $namepro, 'typeuudai' => $uudai, 'bankid' => $bankid);
	return $con -> getvalueall($tbl, $fn, $condetion, $flike, 'datecreate');
}

function int_to_date($int) {
    $time  = date("d/m/Y", $int);
    return $time;
}

$productname = "";
$idbank = "";
$iduudai = "";
$saphethan = "";
$ishethan = "";
$saphethans = "";
if(isset($_GET['tensanpham']) && isset($_GET['selectbank']) && isset($_GET['service_id'])) {
	$productname = trim($_GET['tensanpham']);
	$idbank = trim($_GET['selectbank']);
	if($idbank == "all") {
		$idbank = "";
	}
	$iduudai = trim($_GET['service_id']);
	if($iduudai == "all") {
		$iduudai = "";
	}
	if(isset($_GET['saphethan'])) {
		$saphethans = trim($_GET['saphethan']);
		if($saphethans == "on") {
			$saphethan = time();
			$ishethan = "0";
		} else if($saphethans == "off") {
			$saphethan = time() - 86400;
			$ishethan = "1";
		}
	}
}

$arruudais = getUuDais($productname, $idbank, $iduudai, $saphethan,$ishethan);
?>

<style>
.searchforlaisuat, .formlaisuatthecontent {
	width: 900px;
	margin-bottom: 5px;
}
.searchforlaisuat select {
	width: 170px;
	height: 30px;
	padding: 5px;
	margin-left: 5px;
	margin-right: 5px;
}

.viewlistuudaiinfo table {
	width: 900px;
	max-width: 900px;
}

table td {
	margin-top: 2px;
	border: 1px solid;
	border-radius: 2px 2px 2px 2px;
}

table td img {
	cursor: pointer;
}

.title {
	text-align: center;
	background-color: #0088cc;
	min-height: 40px;
	-moz-border-radius: 8px 8px 0px 0px;
	-webkit-border-radius: 8px 8px 0px 0px;
	border-radius: 8px 8px 0px 0px;
	color: #FFF;
}

.stylenameproduct {
	padding-left: 5px;
	padding-right: 5px;
	width: 150px;
	max-width: 150px;
}

.stylelogobank {
	width: 100px;
	text-align: center;
	max-width: 100px;
}

.stylegiamgia {
	width: 60px;
	text-align: center;
	max-width: 60px;
}

.styletimeth {
	width: 130px;
	text-align: center;
	max-width: 130px;
}

.styleacction {
	width: 80px;
	text-align: center;
	max-width: 80px;
}

.styledatecreateth {
	width: 100px;
	max-width: 100px;
	text-align: center;
}

.nameformtd {
	background: url(../images/bg-login.png);
	-moz-border-radius: 2px 2px 2px 2px;
	-webkit-border-radius: 2px 2px 2px 2px;
	border-radius: 2px 2px 2px 2px;
	text-align: center;
}

table tr:hover td {
	background: #FFC;
}

.stylestt {
	width: 40px;
}
</style>
<h3>Danh Sách Ưu Đãi</h3>
<div style="color: red;">
<?php
	if(isset($_GET['mess'])) {
		echo($_GET['mess']);
	}
?>
</div>
<div class="searchforlaisuat">
<form action="admin.php" method="get">
	<input type="hidden" value="quanlyuudai" name="content" />
	<h4 style="color:#03F; margin-bottom: -10px;">Lọc Theo:</h4><br>
    <label for="tensanpham">Tên Dịch Vụ</label><input type="text" name="tensanpham" class="inputtextfield tensanpham" /><br/>
	<label for="selectbank">Ngân Hàng</label>
	<select name="selectbank" class="selectbank">
    	<option value="all">----Chọn Ngân Hàng----</option>
    </select>
	<label for="service_id">Loại Ưu Đãi</label>
	<select id="ServiceID" name="service_id" class="service_id">
        <option value="all" selected="selected">---Chọn Loại Ưu Đãi---</option>   
        <option value="thoi-trang">Thời Trang</option>
        <option value="du-lich">Du Lịch - Khách sạn</option>
        <option value="am-thuc">Ẩm Thực</option>
        <option value="suc-khoe">Sức Khoẻ - Làm đẹp</option>
        <option value="mua-sam">Siêu thị - TTTM</option>
        <option value="dien-may">Điện máy - Nội thất</option>
        <option value="giai-tri">Giải trí</option>
        <option value="linh-vuc-khac">Lĩnh vực khác</option>        
    </select>
    <label for="saphethan">Hạn ưu đãi</label>
    <select id="saphethan" name="saphethan" class="saphethan">
        <option value="all" selected="selected">---Chọn Hạn ưu đãi---</option>   
        <option value="on">Còn Hạn</option>
        <option value="off">Hết Hạn</option>     
    </select>
    <input type="submit" name="submit" value="Lọc" style="border-radius: 5px; padding:5px; width:50px; background:#03F; color:#FFF; font-weight:bold;" />
</form>
</div><hr /><br />
<div class="viewlistuudaiinfo">
	<form action="../src/control/adminuudai.php" method="post" onsubmit="return confirm('Bạn có muốn xóa danh sách đã chọn?');">
    <div><input type="hidden" name="acc" value="del" /><input type="submit" value="Xóa Danh Sách Đã Chọn" style="border-radius: 5px; padding:5px; width:200px; background:#03F; color:#FFF; font-weight:bold;" /></div><br />
	<table>
    	<tr>
        	<th class="title" style="width:10px;"><input type="checkbox" class="checkall" title="Chọn Tất Cả" /></th>
            <th class="stylestt title">STT</th>
        	<th class="stylenameproduct title">Tên Dịch Vụ</th>
            <th class="stylenameproduct title">Loại DV</th>
            <th class="stylegiamgia title">Giảm Giá</th>
            <th class="styledatecreateth title">Thời Gian</th>
            <th class="stylelogobank title">Ngân Hàng</th>
            <th class="styledatecreateth title">Ngày Cập Nhật</th>
            <th class="styleacction title">Chức Năng</th>
        </tr>
        <?php
		if(count($arruudais) > 0) {
			$i = 0;
			foreach($arruudais as $entry) {
				$i++;
		?>
        <tr>
        	<td class="nameformtd"><input title="Chọn để xóa" type="checkbox" value="<?php echo($entry[0]) ?>" name="deluudai[]" class="linecheckall" /></td>
        	<td class="nameformtd" align="left"><?php echo($i) ?></td>
        	<td class="nameformtd" align="left"><?php echo($entry[3]) ?></td>
            <td class="nameformtd" align="left"><?php echo($entry[2]) ?></td>
            <td class="nameformtd"><?php echo($entry[5]) ?>%</td>
            <td class="nameformtd"><?php echo(int_to_date($entry[6])) ?><br />Đến<br /><?php echo(int_to_date($entry[7])) ?></td>
            <td class="nameformtd"><img width="100px" src="../<?php echo($entry[12]) ?>" title="<?php echo($entry[11]) ?> (<?php echo($entry[10]) ?>)" /></td>
            <td class="nameformtd"><?php echo(int_to_date($entry[15])) ?></td>
            <td class="nameformtd"><img class="class-edituudai-link" src="../images/icon-edit.gif" title="Chỉnh Sửa Ưu Đãi <?php echo($entry[3]) ?>" idedit="<?php echo($entry[0]) ?>" />&nbsp; | &nbsp;<img class="class-deleteuudai-link" src="../images/icon-delete.gif" title="Xóa Ưu Đãi <?php echo($entry[3]) ?>" idedit="<?php echo($entry[0]) ?>" /></td>
        </tr>
        <?php
			}
		} else {
		?>
        <tr><th colspan="7">Chưa có ưu đãi nào trong danh sách</th></tr>
        <?php	
		}
		?>
    </table>
    <br />
    <div><input type="hidden" name="acc" value="del" /><input type="submit" value="Xóa Danh Sách Đã Chọn" style="border-radius: 5px; padding:5px; width:200px; background:#03F; color:#FFF; font-weight:bold;" /></div>
    </form>
</div>
<div><a href="admin.php?content=quanlyuudai&&as=danguudai"><img src="../images/icon-add.jpg" title="Thêm Ưu Đãi Mới" />Thêm Ưu Đãi</a></div>

<script>
loadbank();
function loadbank() {
	$.ajax({
		url: "../src/ajax/ajaxbank.php",
		type: "post",
		data: {act:'getbanks'},
		dataType:"json",
		success: function(response) {
			html = '';
			$.each(response, function(entryIndex, entry){
				html += '<option value="' + entry[0] + '">' + entry[1] + '(' + entry[0] + ')</option>';
			});
			$('.selectbank').append(html);
			$(".selectbank option[value='<?php echo($idbank) ?>']").attr('selected', 'selected');
		}
	});
}
$('.tensanpham').val('<?php echo($productname) ?>');
$(".service_id option[value='<?php echo($iduudai) ?>']").attr('selected', 'selected');
$(".saphethan option[value='<?php echo($saphethans) ?>']").attr('selected', 'selected');


$(document).on('click', '.class-edituudai-link', function(e) {
	elem = $(this)
	var id = elem.attr("idedit");
	window.location= 'admin.php?content=quanlyuudai&&as=edituudai&&id=' + id;
});

$(document).on('click', '.class-deleteuudai-link', function(e) {
	elem = $(this)
	var id = elem.attr("idedit");
	if(confirm("Bạn có muốn xóa?")) {
		$.ajax({
			url: "../src/ajax/ajaxuudai.php",
			type: "post",
			data: {act:'del', idre: id},
			dataType:"json",
			success: function(data) {
				if(data == true) {
					elem.parent().parent().fadeOut(300, function(){elem.parent().parent().remove();});
				} else {
					alert("Cập nhật dữ liệu không thành công");
				}
			}
		});
	}
});

$(document).on('click', '.checkall', function(e) {
	if(!$(this).is(':checked')) {
		$('.checkall').attr('title', 'Chọn Tất Cả');
		$('.linecheckall').removeAttr('checked');
	} else {
		$('.checkall').attr('title', 'Bỏ Chọn Tất Cả');
		$('.linecheckall').attr('checked', 'checked');
	}
});
/*
$('table tr td').not(':first').click(function(e) {
    temp = $(this).parent().children().children().attr('checked') + "";
	if(temp == 'undefined') {
		$(this).parent().children().children().attr('checked', 'checked');
	} else {
		$(this).parent().children().children().removeAttr('checked');
	}
});

$(document).on('click', 'table td', function(e) {
	temp = $(this).parent().children().children().attr('checked') + "";
	if(temp == 'undefined') {
		$(this).parent().children().children().attr('checked', 'checked');
	} else {
		$(this).parent().children().children().removeAttr('checked');
	}
});
*/
</script>